<?php
	header ('Access-Control-Allow-Origin: *');
	header ('Access-Control-Allow-Methods: POST');

	include ('function/conn.php');
	include ('function/function.php');

	function getBookLists($uid, $limit = 0, $page = 0) {
		$limit = (!$limit ? 0 : !is_numeric($limit) ? 0 : $limit) * 1;
		$page = (!$page ? 0 : !is_numeric($page) ? 0 : $page) * 1;

		if (!$uid || empty($uid)) {
			$returnObject = (object) array(
				"error_code" => 2000,
				"error_msg" => "参数错误：uid不能为空",
			);

			return $returnObject;
		}

		$booklists_query = array("book_lists");
		$booklists_object = array();
		$booklists_paging = array();
		$limit_string = "";
		$booklists_counts = 0;
		$booklists_total_counts = 0;

		//获取小说列表总数
		$query = mysql_query("select count(*) from `book-lite_list` where uid='$uid'");

		if ($rs = mysql_fetch_array($query)) {
			$booklists_total_counts = (int) $rs[0];
		}

		//设置分页信息
		if ($limit > 0 && $limit < $booklists_total_counts) {
			if ($booklists_total_counts > 1) {
				$limit_string = "limit " . $page * $limit . ", {$limit}";

				for ($count = 0; $count < ceil($booklists_total_counts / $limit); $count++) {
					$temp[] = array(
						"start" => $count * $limit,
						"limit" => $limit
					);
				}

				$booklists_paging = array(
					"current_page" => $page,
					"paging_counts" => ceil($booklists_total_counts / $limit)
					//"pages" => $temp
				);
			}
		}

		//读取小说列表和历史记录
		for ($count = 0; $count < count($booklists_query); $count++)
		{
			$result = mysql_query("select * from `book-lite_list` where uid='$uid' order by last_read desc {$limit_string}");

			while ($row = mysql_fetch_assoc($result))
			{
				$temp = array(
					"bid" => $row['bid'],
					"title" => $row['title'],
					"author" => $row['author'],
					"history" => array()
				);

				$result_history = mysql_query("select * from `book-lite_history` where uid='$uid' and bid='$row[bid]' limit 1");

				if ($row_history = mysql_fetch_assoc($result_history)) {
					$temp['history'] = array(
						"article_title" => $row_history['article_title'],
						"article_id" => $row_history['article_id']
					);
				}

				$booklists_object[] = $temp;
				$booklists_counts += 1;
			}
		}

		mysql_free_result($query);
		@mysql_free_result($result_history);
		mysql_free_result($result);

		$returnObject = (object) array (
			"total_counts" => $booklists_total_counts,
			"list_counts" => $booklists_counts,
			"lists" => $booklists_object,
			"paging" => $booklists_paging
		);

		return $returnObject;
	}
?>